// pages/uploadMenu/uploadMenu.js
var util = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
    //获取今天日期
    getToday(){
      var _this = this;
      var date = new Date();
      var y = date.getFullYear();
      var m = (date.getMonth()+1)>9?date.getMonth()+1:'0'+(date.getMonth()+1);
      var d = date.getDate()>9?date.getDate():'0'+date.getDate();
      var day = `${y}-${m}-${d}`;
      var zhou = _this.getDay(day);
      _this.setData({
          date:day,
          week:zhou,
          startDate:day
      })
        console.log(date);
    },
    //获取周几
    getDay(day){
        var _this = this;
        var d = (day==7)?0:day;
        var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        var zhou = weekday[d];
        _this.setData({
            week:zhou
        })

    },
    bindDateChange: function(e) {
        // console.log('picker发送选择改变，携带值为', e.detail.value);
        var zhou = this.getDay(e.detail.value);
        this.setData({
            date: e.detail.value,
            week:zhou
        })
    },
    menuContent(e){
       var input = e.detail.value;
        console.log(input);
        var _this = this;
       _this.setData({
           menu_content:input
       })
    },
    getMenu(day){
      var _this = this;
      util.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food`,res=>{
          console.log(res);
          if(res.data){
              _this.setData({
                  menu_content:res.data[0].content
              })
          }
      },{
          id:day
      })
    },
    newMenu(){
      var _this = this;
      var content = _this.data.menu_content;
      var date = _this.data.date;
        console.log(date,content);
        var pro = {
            id:date,
            content:content
        }
        util.getAjax(`${app.globalData.apiUrl}/Api/Food/save_food`,res=>{
            if(res.data == 1){
                util.showToast('修改成功','success',1000);
                setTimeout(function(){
                    wx.navigateBack({
                        delta:1
                    })
                },1000)
            }
         },{
            pro:pro
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      this.setData({
          date:options.date
      })
      util.setNavBarTitle('上传菜单')
      this.getDay(options.date);
      this.getMenu(options.date)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
